package com.boliq.demo.receiver;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import java.util.ArrayList;
import java.util.List;

import blq.ssnb.baseconfigure.simple.MenuBean;
import blq.ssnb.baseconfigure.simple.SimpleMenuActivity;
import blq.ssnb.snbutil.SnbLog;
import blq.ssnb.snbutil.SnbToast;

/**
 * <pre>
 * ================================================
 * 作者: BLQ_SSNB
 * 日期：2021/2/26
 * 邮箱: blq_ssnb@outlook.com
 * 修改次数: 1
 * 描述:
 *      添加描述
 * ================================================
 * </pre>
 */
public class BroadcastActivity extends SimpleMenuActivity {

    private SequenceReceiver mReceiver;
    private IntentFilter mIntentFilter;

    @Override
    protected void initData() {
        super.initData();
        mIntentFilter = new IntentFilter();
        mIntentFilter.addAction("com.boliq.ssnb.dynamic");
        mIntentFilter.addAction("com.boliq.demo.sequence");
    }

    @Override
    protected String navigationTitle() {
        return "广播";
    }

    @Override
    protected List<MenuBean> getMenuBeans() {
        List<MenuBean> list = new ArrayList<>();
        list.add(new MenuBean()
                .setMenuTitle("发送静态广播")
                .setMenuSubTitle("8.0需要 调用 Intent.setPackage()方法设置包名")
                .setOnClickListener(v -> {
                    Intent intent = getBroadIntent("静态广播", "com.boliq.demo.static");
                    intent.setPackage(getPackageName());
                    sendBroadcast(intent);
                }));
        list.add(new MenuBean()
                .setMenuTitle("注册动态广播")
                .setMenuSubTitle("registerReceiver(Receiver, IntentFilter)")
                .setOnClickListener(v -> {
                    if (mReceiver == null) {
                        mReceiver = new SequenceReceiver();
                        registerReceiver(mReceiver, mIntentFilter);
                        SnbToast.showSmart("注册成功");
                    } else {
                        SnbToast.showSmart("不要重复注册");
                    }
                })
        );
        list.add(new MenuBean()
                .setMenuTitle("发送动态广播")
                .setMenuSubTitle("")
                .setOnClickListener(v -> sendBroadcast(getBroadIntent("我是动态广播", "com.boliq.ssnb.dynamic")))
        );
        list.add(new MenuBean()
                .setMenuTitle("注销动态广播")
                .setMenuSubTitle("")
                .setOnClickListener(v -> {
                    if (mReceiver != null) {
                        unregisterReceiver(mReceiver);
                        mReceiver = null;
                        SnbToast.showSmart("注销成功");
                    } else {
                        SnbToast.showSmart("不要重复注销");
                    }
                })
        );
        list.add(new MenuBean()
                .setMenuTitle("显式广播")
                .setMenuSubTitle("显示广播,intent.setComponent(new ComponentName(getPackageName(), StaticReceiver.class.getCanonicalName()));")
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = getBroadIntent("我是显式广播", "我是没注册进去的action");
                        intent.setComponent(new ComponentName(getPackageName(), StaticReceiver.class.getCanonicalName()));
                        sendBroadcast(intent);

                    }
                })
        );

        list.add(new MenuBean()
                .setMenuTitle("无序广播")
                .setOnClickListener(v -> {
                    Intent intent = getBroadIntent("无序广播", "com.boliq.demo.sequence");
                    intent.setPackage(getPackageName());
                    sendBroadcast(intent, null);
                })
        );

        list.add(new MenuBean()
                .setMenuTitle("顺序广播-无拦截")
                .setOnClickListener(v -> {
                    Intent intent = getBroadIntent("顺序广播-无拦截", "com.boliq.demo.sequence");
                    intent.setPackage(getPackageName());
                    sendOrderedBroadcast(intent, null);
                })
        );
        list.add(new MenuBean()
                .setMenuTitle("顺序广播-有拦截")
                .setOnClickListener(v -> {
                    Intent intent = getBroadIntent("顺序广播-拦截第一个", "com.boliq.demo.sequence");
                    intent.putExtra("index", 0);
                    intent.setPackage(getPackageName());
                    sendOrderedBroadcast(intent, null);
                })
        );

        return list;
    }

    private Intent getBroadIntent(String msg, String action) {
        Intent intent = new Intent();
        intent.setAction(action);
        intent.putExtra("msg", msg);
        return intent;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mReceiver != null) {
            unregisterReceiver(mReceiver);
        }
    }
}
